package com.example.highway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.ReportDetailDao;
import com.example.highway.dao.TaskAssignDao;
import com.example.highway.entity.ReportDetail;
import com.example.highway.entity.TaskAssign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("report-detail")
public class ReportDetailController {
    @Autowired
    ReportDetailDao reportDetailDao;

    @RequestMapping("add")
    public MyJson add(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

        try {
            ReportDetail reportDetail = reportDetailDao.save(jsonObject.toJavaObject(ReportDetail.class));
            JSONObject rtn = new JSONObject();
            rtn.put("id", reportDetail.getId());

            return new MyJson("0", rtn);
        }catch (Exception e){
            JSONObject rtn = new JSONObject();
            String errMsg = "lack of key";
            rtn.put("msg", errMsg);
            return new MyJson("1", rtn);
        }



    }

    @RequestMapping("get")
    public MyJson one(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

        ReportDetail reportDetail = jsonObject.toJavaObject(ReportDetail.class);
        List<ReportDetail> reportDetails = reportDetailDao.findAll(Example.of(reportDetail));
        return new MyJson("0", JSONArray.toJSON(reportDetails));

    }
}
